package com.example.beautymakeup.controller;

import com.example.beautymakeup.domain.Code;
import com.example.beautymakeup.domain.PageBean;
import com.example.beautymakeup.domain.Result;
import com.example.beautymakeup.pojo.Hot;
import com.example.beautymakeup.pojo.Order;
import com.example.beautymakeup.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @GetMapping
    public Result getByUserId(@RequestHeader("Shop_token") String token) throws Exception {
        PageBean<Order> pageBean = orderService.getByUserId(token);
        if (pageBean != null){
            return new Result(Code.GET_OK,"获取成功",pageBean);
        }
        return new Result(Code.GET_ERR,"获取失败");

    }


    @GetMapping("/{uid}")
    public Result getByUid(@RequestHeader("Shop_token") String token,@PathVariable("uid") int uid) throws Exception {

        Order byUid = orderService.getByUid(token, uid);
        if (byUid != null){
            return new Result(Code.GET_OK,"获取成功",byUid);
        }
        return new Result(Code.GET_ERR,"获取失败");

    }

    @GetMapping("/douyin/{orderNo}")
    public Result queryDouyinOrder(@RequestHeader("Shop_token") String token, @PathVariable String orderNo) throws Exception {
        //项目中用来验证的token
        //Todo @RequestHeader("Shop_token") String token 待完善不用你改 请不要动
        return orderService.queryDouyinOrder(orderNo);
    }

    @GetMapping("/all")
    public Result getAll(@RequestHeader("Shop_token") String token, @RequestParam("page") int page, @RequestParam("size") int size) throws Exception {
        PageBean<Order> all = orderService.getAll(token, page, size);
        if (all != null){
            return new Result(Code.GET_OK,"获取成功",all);
        }
        return new Result(Code.GET_ERR,"获取失败");
    }

    @GetMapping("/status/{status}")
    public Result getByStatus(@RequestHeader("Shop_token") String token, @PathVariable("status") int status, @RequestParam("page") int page, @RequestParam("size") int size) throws Exception {
        PageBean<Order> all = orderService.getByStatus(token,status, page, size);
        if (all != null){
            return new Result(Code.GET_OK,"获取成功",all);
        }
        return new Result(Code.GET_ERR,"获取失败");
    }


    @PutMapping("/update")
    public Result update(@RequestHeader("Shop_token") String token,@RequestBody Order order) throws Exception {
        boolean update = orderService.update(token, order);
        if (update){
            return new Result(Code.UPDATE_OK,"修改成功");
        }
        return new Result(Code.UPDATE_ERR,"修改失败");
    }

    @PutMapping("/refund")
    public Result refund(@RequestHeader("Shop_token") String token,@RequestBody Order order) throws Exception {
        boolean update = orderService.refund(token, order);
        if (update){
            return new Result(Code.UPDATE_OK,"已申请退款，待审核");
        }
        return new Result(Code.UPDATE_ERR,"修改失败");
    }
    @PostMapping
    public Result add(@RequestHeader("Shop_token") String token,@RequestBody Order order) throws Exception {
        boolean add = orderService.add(token, order);
        if (add){
            return new Result(Code.SAVE_OK,"增加成功");
        }
        return new Result(Code.SAVE_ERR,"增加失败");
    }
}
